/*
 * Created by Lettle on 2022/3/21.
 * QQ: 1071445082
 * github: https://github.com/Python-Lettle
 * gitee : https://gitee.com/lettle/
 */
#include <stdlib.h>
#include "cot_error.h"
#include "cot_array.h"

/***********************************************************
 *  功能:		重新分配动态数组容量
 *  parr:		动态数组指针
 *  new_size:	动态数组最新元素个数
 **********************************************************/
void dynarray_realloc(cot_array *parr, int new_size)
{
    int capacity;
    void *data;

    capacity = parr->capacity;
    while (capacity < new_size)
        capacity = capacity * 2;
    data = realloc(parr->data, capacity);
    if (!data)
        error("内存分配失败");
    parr->capacity = capacity;
    parr->data = data;
}

void array_add(cot_array *parr, void *data)
{

}

void array_init(cot_array *parr,int initsize)
{
    if(parr != NULL) {
        parr->count = 0;
        parr->capacity = initsize;
        parr->data = (void **) malloc(sizeof(void *) * initsize);
    }
}
void array_free(cot_array *parr)
{
    void **p;
    for(p=parr->data;parr->count;++p, --parr->count) {
        if(*p)
            free(*p);
    }
    free(parr->data);
    parr->data = NULL;
}
int array_search(cot_array *parr, int key)
{
    int i, **p;
    p = (int **) parr->data;
    for(i=0; i<parr->count; i++, p++)
        if (key==**p)
            return i;
    return -1;
}